const excludeKeys = ["price", "quantity","weight","sn","id"]; // 指定要排除的属性键 
export function getProperty(obj,excludeKeys){
  const result = Object.fromEntries(
    Object.entries(obj).filter(([key]) => !excludeKeys.includes(key))
  );
  return result;
}
/**
 * 将两个字符串（如 "红色,富士" 和 "富士,红色"）视为相同的值
 * @param {Object} str
 */
function normalizeStr(str) {
  return str.split(',').map(item => item.trim()).sort().join(',');
}

/**
 *构建规格列表参数
 */
export function buildSpecList(specGoodsList){
  //const excludeKeys = ["price", "quantity","weight","sn"]; // 指定要排除的属性键
  let arry = [];
  for (var i = 0; i < specGoodsList.length; i++) {
    const item = specGoodsList[i];

    let itemData = {};
    const data = getProperty(item,excludeKeys);
    itemData.specs = JSON.stringify(data);
    itemData.price = item.price;
    itemData.quantity = item.quantity;
    itemData.weight = item.weight;
    itemData.sn = item.sn;

    arry.push(itemData);
  }
  return arry;
}

/**
 * 将list数据放入map,key为对象的属性值逗号拼接，例如：key:'红色,品种'
 * @param {Object} paramList
 */
export function putToMap(paramList){
	//const excludeKeys = ["price", "quantity","weight","sn"]; // 指定要排除的属性键
	// 创建一个 Map 对象
	const dataMap = new Map();
	paramList.forEach(item => {
		let itemValueArr = [];
		const dataItem = getProperty(item,excludeKeys);
		for (let key in dataItem) {
		  if (dataItem.hasOwnProperty(key)) { // 检查属性是否属于对象自身，而不是继承的
        itemValueArr.push(dataItem[key]);
		  }
		}
		const myKey = normalizeStr(itemValueArr.join());
		dataMap.set(myKey,item);
	});
	return dataMap;
}

export function buildDataKey(dataObj){
	//const excludeKeys = ["price", "quantity","weight","sn","id"]; // 指定要排除的属性键
	const dataItem = getProperty(dataObj,excludeKeys);
  let itemValueArr = [];
  for (let key in dataItem) {
    if (dataItem.hasOwnProperty(key)) { // 检查属性是否属于对象自身，而不是继承的
      itemValueArr.push(dataItem[key]);
    }
  }
  const myKey = normalizeStr(itemValueArr.join());
	return myKey;
}
